﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class 数据算法_以_分開的樹型結果 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = (new Tong.TongTreeDataTable()).TreeTable
            //("V1", "DH", "DH", "DHName", "Pid");
            ("CX_Column","CX_Column_id", "CX_Column_id", "CX_Column_Name", "CX_Column_followid");

        
        GridView1.DataBind();

        Response.Write(ShowTree());


    }









    #region 显示树的XML文件 调用方法 ShowTree()

    /// <summary>
    /// SelectXML 方法要输出的XML文本文件 使用请
    /// </summary>
    string TreeXmlText = "<?xml version='1.0' encoding='utf-8'?>\r<系统菜单>";

    Tong.TongUse TextData = new Tong.TongUse();
    /// <summary>
    /// 写XML层次函数
    /// </summary>
    /// <param name="parentId">参数parentId 递归从属 ID</param>
    /// <param name="layer">参数parentId 递归 层次ID 相当于XML文件的层次结构 </param>
    protected void SelectXML(string parentId, int layer)
    {
        int i_n = 0, j;
        string cId;
        string sNbsp = "";//输出的空格 \t 为TAB 

        //建立记录集  相当于ASP 中的rs 

        DataTable Myds = TextData.SqlataSet("select Dh as  ClassName ,DHNAme as URL  from [V1] where Pid='" + parentId + "'").Tables[0];//调用存贮过程创建DataTalbe对象
       


        j = Myds.Rows.Count;// Myds.Rows.Count记录集中的记录数

        for (int i = 0; i <= (layer - 1); i++)//输入TAB 显示层次
        {
            sNbsp += "\t \t";
        }

        //TreeXmlText += "<一级分类菜单 ";//输出XML



        for (int i = 0; i < j; i++)
        {

            cId = Myds.Rows[i]["ClassName"].ToString();//得到现在行的索引值;相当于ASP rs("id")

            DataTable MyDt = TextData.SqlataSet("select Dh as  ClassName ,DHNAme as URL from [V1] where DH='" + cId + "'").Tables[0];//建立从属记录集

            if (MyDt.Rows.Count > 0)//看看有是不是不只一条记录
            {


                TreeXmlText += sNbsp + cId + Myds.Rows[0]["ClassName"].ToString() + "<一级分类菜单  分类菜单名='" + MyDt.Rows[0]["ClassName"].ToString() + "' 显示url='~/" + TextData.CodeUrlText(MyDt.Rows[0]["URL"].ToString()) + "' Trang='main'>\n";



            }
            else//如果只有一条记录
            {

                TreeXmlText += sNbsp + cId + Myds.Rows[0]["ClassName"].ToString() + "<一级分类菜单 分类菜单名='" + Myds.Rows[0]["ClassName"].ToString() + "' 显示url='~/" + TextData.CodeUrlText(Myds.Rows[0]["URL"].ToString()) + "' Trang='main'>\n";
            }


            SelectXML(cId, (layer + 1));//调用函数本身执行递归

            TreeXmlText += sNbsp + "</一级分类菜单>\n";


        }






        // TreeXmlText += "</一级分类菜单>\n"; 一定要删掉　不然ＸＭＬ文件无法显示
    }

    /// <summary>
    /// 返回XML字符
    /// </summary>
    public String ShowTree()
    {
        SelectXML("0", 0);
        TreeXmlText += "</系统菜单>";
        return TreeXmlText;
    }
    #endregion
}
